﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_Thumbnail : BasePostPage
{
    private Role.Roles[] EditRoles = new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher };

    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        dsPost.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsPost_SelectData);
        dsPost.UpdateData += new EventHandler<Acms.DataControls.UpdateDataEventArgs<Post>>(dsPost_UpdateData);
        dvPost.ItemUpdated += new DetailsViewUpdatedEventHandler(dvPost_ItemUpdated);
        if (!IsPostBack)
        {
            lnkEdit.Visible = User.HasRole(EditRoles);
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
            DataBind();
        }
    }

    protected override string PageTitle
    {
        get { return "PostSummary"; }
    }

    void dsPost_UpdateData(object sender, Acms.DataControls.UpdateDataEventArgs<Post> e)
    {
        try
        {
            var upload = dvPost.FindControl("upload") as FileUpload;
            var cboRemove = dvPost.FindControl("cboRemove") as CheckBox;

            if (upload != null && upload.HasFile)
            {
                e.Item.BeginTransaction();

                Acms.Bll.File file = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Image, upload.PostedFile, upload.FileBytes);

                file.UseTransaction(e.Item.TransactionKey);
                file.Update();
                e.Item.ThumbnailId = file.Id;
            }
            else if (cboRemove != null && cboRemove.Checked && e.Item.ThumbnailId != null)
            {
                //cant delete thumb beacuse published version of post may be using it etc.
                e.Item.ThumbnailId = null;
            }

            e.Item.RequiresPublishing = true;
            e.Item.Update();
            if (e.Item.TransactionKey != Guid.Empty)
            {
                e.Item.CommitTransaction();
            }
        }
        catch
        {
            if (e.Item.TransactionKey != Guid.Empty)
            {
                e.Item.RollbackTransaction();
            }
            throw;
        }
    }

    void dvPost_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
    }

    void dsPost_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        e.SetData(CurrentPost);
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        dvPost.UpdateItem(true);

    }
    
    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
        dvPost.ChangeMode(DetailsViewMode.Edit);
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        lnkEdit.Visible = User.HasRole(EditRoles);
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
        dvPost.ChangeMode(DetailsViewMode.ReadOnly);
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Preview", this.AcmsContext.OriginalUrl);
    }

}
